using IcsSEMCtrl.Db;
using IcsSEMCtrl.Device.Observe;
using IcsSEMCtrl.Global.Device;
using IcsSEMCtrl.Service.Logger;
using IcsSEMCtrl.Service.Material;
using IcsSEMCtrl.Util.Client;
using Lifter.Device.Dispatcher;
using Lifter.Event.Lifter;
using Lifter.Global;
using Lifter.Global.Device;
using Lifter.Utility;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Threading;
using System.Timers;
using TKT.Lifter.CIM.Lifter.Device.Unit;
using TKT.Lifter.CIM.Lifter.Event.Lifter;
using TKT.Lifter.CIM.Lifter.Gui.Monitor.Panels;
using static TKT.Lifter.CIM.Lifter.Device.Unit.ShuttleMaster;

namespace Lifter.Device.Unit
{
    public class Vehicle : LocationUnit
    {
        private enum enumCIMVehicleSetResetCase
        {
            FAULT_RESET,
            TRANSFER_REQ,
            RUN_REQ,
            DOWN_REQ,
            VEHICLEMOVE_REQ,
            START_TO_SRC_ACK,
            ARRIVED_AT_SRC_ACK,
            LOAD_START_ACK,
            LOAD_COMPLETE_ACK,
            START_TO_DEST_ACK,
            ARRIVED_AT_DEST_ACK,
            UNLOAD_START_ACK,
            UNLOAD_COMPLETE_ACK,
            SRC_ABNORMAL_ACK,
            DEST_ABNORMAL_ACK
        }
        public static class Constants
        {
            public const string FRONT = "Front";
            public const string BEHIND = "Behind";
        }
        private const string CLASS = "Vehicle";
        private bool isOutofService = false;
        private LifterSEM hostDevice = null;
        private enumRIDState vehicleR_I_D = enumRIDState.DOWN;//轿厢状态
        private VehicleState vehiclestate = VehicleState.NONE;
        private short vehicleLocation = 0;
        private VehicleTransferStep vehicletransferStep = VehicleTransferStep.INIT;
        private bool isOrderEnable = false;
        private bool isCSTPresenceOn = false;
        private string carrierID = "";
        private string commandId = "";
        private int from = 0;
        private int to = 0;
        private enumFault fault = enumFault.FAULT_RESET;
        private int waitForRunCount = 0;//下发搬送指令到入轿厢这段时间计时
        private int vehicleMoveTo = 0;
        private string rptAlarmID = "";
        private string rptAlarmText = "";
        public bool isTRANSFERREQ = false;
        private bool isTransferOutAction = false;//判断轿厢buffer是否为出料动作

        //-----------------
        private ShuttleWorkMode shuttleWorkMode;//轿厢工作模式
        private string shuttleAroundPosition;//轿厢前后位置方向
        private bool shuttleTransferReady; //轿厢搬送准备就绪
        private int vehicleSource; //轿厢搬送指令起始地
        private int vehicleDest; //轿厢搬送指令目的地
        private ShuttleMaster shuttleMaster;
        public delegate void SenserInfoDelegate(int value);
        public SenserInfoDelegate senserInfoDelegate;
        public bool IsOutofService
        {
            get
            {
                return this.isOutofService;
            }
            set
            {
                this.isOutofService = value;
            }
        }
        public LifterSEM HostDevice
        {
            get
            {
                if (this.hostDevice == null)
                {
                    this.hostDevice = (LifterSEM)Naming.GetDeviceManager().GetDevice();
                    this.ShuttleMaster = this.HostDevice.ShuttleMaster;
                }
                return this.hostDevice;
            }
            set
            {
                this.hostDevice = value;
            }
        }
        public enumRIDState VehicleR_I_D
        {
            get
            {
                return this.vehicleR_I_D;
            }
            set
            {
                this.vehicleR_I_D = value;
            }
        }
        public VehicleState Vehiclestate
        {
            get
            {
                return this.vehiclestate;
            }
            set
            {
                this.OnVehicleStateChange(value);
                this.vehiclestate = value;
            }
        }
        public short VehicleLocation
        {
            get
            {
                return this.vehicleLocation;
            }
            set
            {
                this.vehicleLocation = value;
            }
        }
        public VehicleTransferStep VehicletransferStep
        {
            get
            {
                return this.vehicletransferStep;
            }
            set
            {
                if (this.waitForRunCount != 0)
                {
                    this.waitForRunCount = 0;
                }
                this.vehicletransferStep = value;
            }
        }
        public bool IsOrderEnable
        {
            get
            {
                return this.isOrderEnable;
            }
            set
            {
                this.isOrderEnable = value;
            }
        }
        public bool IsCSTPresenceOn
        {
            get
            {
                return this.isCSTPresenceOn;
            }
            set
            {
                this.isCSTPresenceOn = value;
            }
        }
        public string CarrierID
        {
            get
            {
                return this.carrierID;
            }
            set
            {
                this.carrierID = value;
            }
        }
        public string CommandId
        {
            get
            {
                return this.commandId;
            }
            set
            {
                this.commandId = value;
            }
        }
        public int From
        {
            get
            {
                return this.from;
            }
            set
            {
                this.from = value;
            }
        }
        public int To
        {
            get
            {
                return this.to;
            }
            set
            {
                this.to = value;
            }
        }
        public enumFault Fault
        {
            get
            {
                return this.fault;
            }
            set
            {
                this.fault = value;
            }
        }
        public int VehicleMoveTo
        {
            get
            {
                return this.vehicleMoveTo;
            }
        }
        public string RptAlarmID
        {
            get
            {
                return this.rptAlarmID;
            }
        }
        public string RptAlarmText
        {
            get
            {
                return this.rptAlarmText;
            }
        }
        #region 轿厢点位
        /// <summary>
        /// 轿厢物料感知信号
        /// </summary>
        public Observable _I_SHUTTLEMATERIALPRESENCE
        {
            get
            {
                return base.Observables["SHUTTLEMATERIALPRESENCE"];
            }
        }
        /// <summary>
        /// 轿厢CV准备接收GET/PUT指令
        /// </summary>
        public Observable _I_SHUTTLECVREADY
        {
            get
            {
                return base.Observables["SHUTTLECVREADY"];
            }
        }
        /// <summary>
        /// 轿厢CV GET/PUT完成指令
        /// </summary>
        public Observable _I_CVGETPUTCOMPLETE
        {
            get
            {
                return base.Observables["CVGETPUTCOMPLETE"];
            }
        }
       
        /// <summary>
        /// 物料类型
        /// </summary>
        public short I_SHUTTLEMATERIALTYPE
        {
            get
            {
                return base.Observables["SHUTTLEMATERIALTYPE"].AsShort;
            }
        }
        /// <summary>
        /// 轿厢transfer指令起始地
        /// </summary>
        public short O_CVFROM
        {
            set
            {
                base.Observables["CVFROM"].AsShort = value;
                this.vehicleSource = value;
            }
        }
        /// <summary>
        /// 轿厢transfer指令目的地
        /// </summary>
        public short O_CVTO
        {
            set
            {
                base.Observables["CVTO"].AsShort = value;
                this.vehicleDest = value;
            }
        }
        /// <summary>
        /// 轿厢CV GET/PUT完成指令回复
        /// </summary>
        public bool O_CVGETPUTCOMPLETERPY
        {
            set
            {
                base.Observables["CVGETPUTCOMPLETERPY"].AsBoolean = value;
            }
        }
        #endregion
        public string I_SHUTTLEMATERIALID
        {
            get
            {
                return base.Observables["SHUTTLEMATERIALID"].AsString;
            }
            set
            {
                base.Observables["SHUTTLEMATERIALID"].AsString = value;
            }
        }
        public short O_VEHICLEMOVELOCATION
        {
            set
            {
                base.Observables["VEHICLEMOVELOCATION"].AsShort = value;
            }
        }

        public bool O_MANUALMATERIALID
        {
            set
            {
                base.Observables["MANUALMATERIALID"].AsBoolean = value;
            }
        }
        public Observable _I_SENSORIO
        {
            get
            {
                return base.Observables["SENSORIO"];
            }
        }
        public short _I_SENSORIOVALUE
        {
            get
            {
                return base.Observables["SENSORIO"].AsShort;
            }
        }
        internal ShuttleWorkMode ShuttleWorkMode { get => shuttleWorkMode; set => shuttleWorkMode = value; }
        public string ShuttleAroundPosition { get => shuttleAroundPosition; set => shuttleAroundPosition = value; }
        public ShuttleMaster ShuttleMaster { get => shuttleMaster; set => shuttleMaster = value; }
        public bool ShuttleTransferReady { get => shuttleTransferReady; set => shuttleTransferReady = value; }
        public bool IsTransferOutAction { get => isTransferOutAction; set => isTransferOutAction = value; }
        public int VehicleSource { get => vehicleSource; set => vehicleSource = value; }
        public int VehicleDest { get => vehicleDest; set => vehicleDest = value; }

        public Vehicle()
        {
        }
        public Vehicle(string elementID, SubSystemType subType, string ownerId, string Equipmentname, string assemblyName, string plcID, string hostID)
            : base(elementID, SubSystemType.VEHICLE, ownerId, "Lifter.Device.Unit.Vehicle", assemblyName, plcID, hostID)
        {
        }
        protected override int Add(DBConnection theDB)
        {
            int result = 0;
            try
            {
                 result = base.Add(theDB);
            }
            catch (SqlException ex)
            {
                LogHelper.loggerError("Vehicle.add流程报错",ex);
                throw ex;
            }
            return result;
        }
        protected override void Delete(DBConnection theDB)
        {
            try
            {
                base.Delete(theDB);
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        protected override void Load(DBConnection theDB)
        {
            try
            {
                base.Load(theDB);//加载LocationUnit信息
                this.LoadShuttleInfo(this.ID);
            }
            catch (SqlException ex)
            {
            }
            finally
            {
            }
        }
        /// <summary>
        /// 获取SHUTTLE表数据信息
        /// </summary>
        private void LoadShuttleInfo(string shuttleID)
        {
            DBConnection dBConnection = DBControl.GetConnection();
            string sqlString = string.Format("SELECT * FROM SHUTTLE WHERE NAME = '{0}'", shuttleID);
            try
            {
                SqlDataReader sqlDataReader = dBConnection.ExecuteQuery(sqlString);
                while (sqlDataReader.Read())
                {
                    this.ShuttleAroundPosition = sqlDataReader["AROUNDPOSITION"].ToString();
                }
                if (sqlDataReader != null)
                {
                    sqlDataReader.Close();
                }
                DBControl.RemoveDBConection(dBConnection);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected override int Save(DBConnection theDB)
        {
            int result = 0;
            try
            {
                result = base.Save(theDB);
            }
            catch (SqlException ex)
            {
                LogHelper.loggerError("Vehicle.save发生错误",ex);
            }
            return result;
        }
        public override void Init()
        {
            base.Init();
            this.AddSubScribe();
            this.CimAllCmdReset();
        }
        public override void Startup()
        {
            base.Startup();
        }
        public override void Shutdown()
        {
            base.Shutdown();
        }
        public void AddSubScribe()
        {
            this._I_SHUTTLEMATERIALPRESENCE.ObservableValueChanged += new DelegateObservableValueChanged(this._I_SHUTTLEMATERIALPRESENCE_ObservableValueChanged);
            this._I_CVGETPUTCOMPLETE.ObservableValueChanged += new DelegateObservableValueChanged(this._I_CVGETPUTCOMPLETE_ObservableValueChanged);
            this._I_SHUTTLECVREADY.ObservableValueChanged += new DelegateObservableValueChanged(this._I_SHUTTLECVREADY_ObservableValueChanged);
            this._I_SENSORIO.ObservableValueChanged += new DelegateObservableValueChanged(_I_SENSORIO_ObservableValueChanged);
/*            foreach (string current in base.Observables.Keys)
            {
                base.Observables[current].ObservableLogEvent += new delegateObservableLogEvent(this.Vehicle_ObservableLogEvent);
            }*/
        }

        private void _I_SENSORIO_ObservableValueChanged(object sender, object value)
        {
            try
            {
                int SensorIOData = Convert.ToInt32(value);
                //委托前端界面实时显示
                if (this.senserInfoDelegate != null)
                {
                    this.senserInfoDelegate(SensorIOData);
                }
            }
            catch (Exception ex)
            {
                LogHelper.loggerError("在方法_I_SENSORIO_ObservableValueChanged中报错", ex);
            }
        }

        /// <summary>
        /// 轿厢CV准备接收GET/PUT指令
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void _I_SHUTTLECVREADY_ObservableValueChanged(object sender, object value)
        {
            this.ShuttleTransferReady = (bool)value;
        }
        /// <summary>
        /// 轿厢CV GET/PUT完成指令
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void _I_CVGETPUTCOMPLETE_ObservableValueChanged(object sender, object value)
        {
            if ((bool)value)
            {
                Console.WriteLine("TRANSFERCOMPLETE" + (bool)value);
                LogHelper.loggerInfo("CVGETPUTCOMPLETE动作信号上报，信号:" + (bool)value);
                //清除指令
                this.O_CVFROM = 0;
                this.O_CVTO = 0;
                //回复点位
                this.O_CVGETPUTCOMPLETERPY = true;

                //判断如果是轿厢出料完成
                bool transferActionOutFlag = this.IsTransferOutAction;
                if (transferActionOutFlag)
                {
                    Console.WriteLine("轿厢出料动作完成");
                    LogHelper.loggerInfo("轿厢出料动作完成");
/*                    //清除物料绑定的指令信息
                    Carrier carrier = Naming.GetCarrierManager().GetCarrier(this.carrierID);
                    if (carrier != null)
                    {
                        carrier.CommandID = "";
                    }
                    else
                    {
                        //记录Log日志，或弹窗提醒
                        LogHelper.loggerInfo("物料出轿厢，清除物料绑定的指令时，物料信息为空");
                    }*/
                }
            }
            else
            {
                this.O_CVGETPUTCOMPLETERPY = false;
                Console.WriteLine("轿厢做Transfer动作完成上报，动作信号:" + (bool)value);
                LogHelper.loggerInfo("轿厢做Transfer动作完成上报，动作信号:" + (bool)value);
            }
        }
        /// <summary>
        /// 当轿厢上有实物上报
        /// 更改物料，指令状态，上报Host CarrierTransferring事件， CarrierLocationChange事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void _I_SHUTTLEMATERIALPRESENCE_ObservableValueChanged(object sender, object value)
        {
            this.isCSTPresenceOn = (bool)value;
            LogHelper.loggerInfo($"轿厢[{this.HostID}]在籍信号上报:" + this.isCSTPresenceOn);
            try
            {
                if (this.isCSTPresenceOn)
                {
                    //轿厢上有实物
                    this.OnVehicleCarrierPresenceOn();
                }
                else
                {
                    //轿厢上实物离开
                    this.OnVehicleCarrierPresenceOff();
                    lock (this.hostDevice.LDispatcher.workList_monitor)
                    {
                        foreach (TransferWorkItem transferWorkItem in this.hostDevice.LDispatcher.workList)
                        {
                            if (transferWorkItem.CommandID.Equals(this.CommandId))
                            {
                                transferWorkItem.IsOnShuttle = false;
                            }
                        }
                    }
                    if (this.shuttleMaster.ShuttleMasterStatus == enumRIDState.DOWN)
                    {
                        Carrier carrier = this.hostDevice.GetCarrierByCarrierID(this.carrierID);
                        if (carrier != null)
                        {
                            carrier.CompleteResult = Carrier.MaterialCompleteTypeConstants.RESULT_NG_CANCEL;
                            this.hostDevice.SecsDriver.SendMessage("S6F11", "308", carrier);
                            Naming.GetCarrierManager().DeleteCarrier(carrier.CarrierId);
                            LogHelper.loggerInfo($"轿厢{this.HostID}上报MCS carrierRemoveComplete(308)事件，并删除本地的物料信息。物料ID为:" + carrier.CarrierId);
                            MaterialInfoControl.materialInfoControl.UpdateDelegate();
                        }
                        else
                        {
                            Console.WriteLine($"轿厢[{this.HostID}]状态为down，且轿厢CV ("+ this.HostID +")在籍Off后，上报MCS carrierRemoveCompelete事件时，物料为空");
                            LogHelper.loggerInfo($"轿厢[{this.HostID}]状态为down，且轿厢CV (" + this.HostID + ")在籍Off后，上报MCS carrierRemoveCompelete事件时，物料为空");
                        }
                    }
                    //清除轿厢绑定的指令信息
                    this.CommandId = "";
                    //清空轿厢绑定的物料信息
                    this.carrierID = "";
                }
                this.InnerPublishEvent(VehicleEvent.VehicleEventType.CST_PRESENCE);
            }
            catch(Exception ex)
            {
                LogHelper.loggerError("在[_I_SHUTTLEMATERIALPRESENCE_ObservableValueChanged]方法中报错",ex);
            }
        }
        private void OnVehicleStateChange(VehicleState value)
        {
            LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            Carrier carrierOnVehicle = null;
            TransferWorkItem workItem = null;
            if (this.carrierID != "")
            {
                carrierOnVehicle = Naming.GetCarrierManager().GetCarrier(this.CarrierID);
            }
            if (this.commandId != null)
            {
                workItem = lifterSEM.LDispatcher.GetJob(this.commandId);
            }
            bool flag = false;
            bool flag2 = false;
            bool isInstalledOLD = false;
            bool isInstalledNew = false;
            flag = this.OnCheckAssignedState(flag);
            flag2 = Vehicle.OnCheckAssignedState(value, flag2);
            if (flag || this.vehiclestate == VehicleState.NOTASSIGNED)
            {
                isInstalledOLD = true;
            }
            if (flag2 || value == VehicleState.NOTASSIGNED)
            {
                isInstalledNew = true;
            }
            this.OnHostReportingVehicleState(value, lifterSEM, carrierOnVehicle, workItem, flag, flag2, isInstalledOLD, isInstalledNew);
            this.InnerPublishEvent(VehicleEvent.VehicleEventType.VEHICLE_STATE);
        }
        /// <summary>
        /// add by yangshl 2023/10/11
        /// 轿厢CV绑定物料ID
        /// 轿厢CV绑定指令ID
        /// 更新物料的当前位置
        /// 更新指令状态为TRANSFERRING
        /// 更新物料状态为TRANSFERRING
        /// </summary>
        private void OnVehicleCarrierPresenceOn()
        {
            //获取PLC的CSTID
            this.carrierID = this.I_SHUTTLEMATERIALID;

            //轿厢物料丢失
            if (string.IsNullOrEmpty(this.CarrierID))
            {
                //异常Case处理
                LogHelper.loggerInfo($"轿厢[{this.HostID}]在籍时，物料ID为空");
                Thread.Sleep(1000);
                this.carrierID = this.I_SHUTTLEMATERIALID;
            }
            else
            {
                CarrierManager carrierManager = Naming.GetCarrierManager();

                if (!carrierManager.carriers.ContainsKey(this.carrierID))
                {
                    Thread.Sleep(1000);
                    this.carrierID = this.I_SHUTTLEMATERIALID;
                }
            }
            try
            {
                /*                //通过当前位置获取物料信息
                                Carrier carrierByLocation = Naming.GetCarrierManager().GetCarrierByLocation(this.HostID);
                                if (carrierByLocation == null)
                                {
                                    //更新CST状态，位置以及指令的状态，位置
                                    this.UpdateCarrierOnVehicle();
                                }
                                else
                                {
                                    carrierByLocation.CompleteResult = Carrier.MaterialCompleteTypeConstants.RESULT_NG_CANCEL;
                                    //当前位置存在物料，则删除
                                    Naming.GetCarrierManager().DeleteCarrier(carrierByLocation.CarrierId);
                                    LogHelper.loggerInfo($"轿厢CV[{this.HostID}]上存在相同的物料，删除相同的物料");
                                    MaterialInfoControl.materialInfoControl.UpdateDelegate();
                                    //更新CST状态，位置以及指令的状态，位置
                                    this.UpdateCarrierOnVehicle();
                                }*/
                Carrier carrier = this.HostDevice.GetCarrierByCarrierID(this.carrierID);
                if (carrier == null)
                {
                    LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetDevice();
                    lifterSEM.InstallMaterial(carrierID, base.HostID, "", CarrierState.TRANSFERRING, CarrierType.CA_TRAY, enumIDReadState.FAILURE.ToString(), Carrier.MaterialCreateTypeConstants.AUTO);
                    //更新指令的当前位置以及传输状态
                    lifterSEM.LDispatcher.DoTransferCarrierIdCommandIdUpdate(carrierID);
                    LogHelper.loggerInfo($"轿厢在籍时，物料ID为空,重新生成物料信息，物料ID为[{this.HostID}]");
                }
                Carrier carrierId = this.HostDevice.GetCarrierByCarrierID(this.carrierID);
                if (carrierId != null)
                {
                    //轿厢绑定指令ID
                    this.CommandId = carrierId.CommandID;
                }
                //更新CST状态，位置以及指令的状态，位置
                this.UpdateCarrierOnVehicle();
            }
            catch(Exception ex)
            {
                LogHelper.loggerError("在[OnVehicleCarrierPresenceOn]方法中报错", ex);
            }
        }
        /// <summary>
        /// 实物离开轿厢,上报Host ShuttleIdle事件
        /// </summary>
        private void OnVehicleCarrierPresenceOff()
        {
/*            bool transferActionOutFlag = this.IsTransferOutAction;
            if (transferActionOutFlag)
            {
                //判断当前轿厢buffer出料指令跟shuttleActive指令是否一致
                if (this.shuttleMaster.ShuttleActiveCommand.CommandID.Equals(this.CommandId))
                {
                    LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetDevice();
                    //上报Host ShuttleIdle事件
                    lifterSEM.SecsDriver.SendMessage("S6F11", "408", this.shuttleMaster);
                    this.ShuttleMaster.ShuttleActiveCommand.CommandID = "";
                }
            }*/
            //改变指令状态，记录历史Log，从指令集合删除此指令
            this.TransferComplete();
            Console.WriteLine($"轿厢[{this.HostID}]出料完成，删除指令[{this.commandId}]，上报TransferComplete事件给MCS");
            LogHelper.loggerInfo($"轿厢[{this.HostID}]出料完成，删除指令[{this.commandId}]，上报TransferComplete事件给MCS");
            if (shuttleMaster.ShuttleMasterStatus == enumRIDState.DOWN)
            {
                this.ShuttleMaster.ShuttleActiveCommand.CommandID = "";
            }
        }
        /// <summary>
        /// 更新轿厢上的CST状态
        /// </summary>
        private void UpdateCarrierOnVehicle()
        {
            try
            {
                LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetDevice();
                Carrier carrier = Naming.GetCarrierManager().GetCarrier(this.carrierID);
                if (carrier != null)
                {
                    //CST状态改变
                    this.HostDevice.CarrierStateChange(carrier, CarrierState.TRANSFERRING);
                    //设置物料当前位置
                    carrier.SetCarrierCurrentLocation(base.HostID);
                    //更新指令的当前位置以及传输状态
                    lifterSEM.LDispatcher.DoTransferCarrierLocUpdate(carrier.CarrierId, base.HostID);
                    lifterSEM.SecsDriver.SendMessage("S6F11", "306", this.HostDevice.ShuttleMaster, carrier);
                    Thread.Sleep(200);
                    //上报MCS carrierLocationChange事件
                    lifterSEM.SecsDriver.SendMessage("S6F11", "305", carrier);
                    LogHelper.loggerInfo("上报MCS carrierLocationChange(305)事件");
                }
                else
                {
                    LogHelper.loggerInfo("在轿厢CV上更新物料信息时，物料信息为空，物料ID:" + this.CarrierID);
                }
            }
            catch (Exception ex)
            {
                LogHelper.loggerError("在方法[UpdateCarrierOnVehicle]中报错",ex);
            }
        }
        private void TransferComplete()
        {
            LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            if (!string.IsNullOrEmpty(this.carrierID))
            {
                
                TransferWorkItem jobByCarrierID = lifterSEM.LDispatcher.GetJobByCarrierID(this.carrierID);
                if (jobByCarrierID != null)
                {
                    lifterSEM.LDispatcher.CompleteJob(jobByCarrierID.CommandID, enumTransferResult.SUCCESS);
                }
                else
                {
                    LogHelper.loggerInfo($"物料[{this.carrierID}]指令为空，无法删除指令");
                }
            }
            else
            {
                LogHelper.loggerInfo("轿厢出料删除指令时，物料ID为空，删除失败");
            }
        }
        private void OnHostReportingVehicleState(VehicleState value, LifterSEM lftSem, Carrier carrierOnVehicle, TransferWorkItem workItem, bool isAssignedOLD, bool isAssingedNew, bool isInstalledOLD, bool isInstalledNew)
        {
            if (this.vehiclestate == VehicleState.ENROUTE && value == VehicleState.PARKED)
            {
            }
            if ((this.vehiclestate == VehicleState.PARKED && value == VehicleState.ACQUIRING) || (this.vehiclestate == VehicleState.DEPOSITING && value == VehicleState.ACQUIRING))
            {
            }
            if (this.vehiclestate == VehicleState.ACQUIRING && value == VehicleState.PARKED)
            {
            }
            if (this.vehiclestate == VehicleState.NOTASSIGNED && isAssingedNew)
            {
            }
            if (this.vehiclestate == VehicleState.PARKED && value == VehicleState.ENROUTE)
            {
            }
            if ((this.vehiclestate == VehicleState.PARKED && value == VehicleState.DEPOSITING) || (this.vehiclestate == VehicleState.ACQUIRING && value == VehicleState.DEPOSITING))
            {
            }
            if (this.vehiclestate == VehicleState.DEPOSITING && value == VehicleState.PARKED)
            {
            }
            if (this.vehiclestate == VehicleState.REMOVED && isInstalledNew)
            {
            }
            if (isInstalledOLD && value == VehicleState.REMOVED)
            {
            }
            if (isAssignedOLD && value == VehicleState.NOTASSIGNED)
            {
            }
        }
        private static bool OnCheckAssignedState(VehicleState value, bool isAssingedNew)
        {
            if (value == VehicleState.ENROUTE || value == VehicleState.PARKED || value == VehicleState.ACQUIRING || value == VehicleState.DEPOSITING)
            {
                isAssingedNew = true;
            }
            return isAssingedNew;
        }
        private bool OnCheckAssignedState(bool isAssignedOLD)
        {
            if (this.vehiclestate == VehicleState.ENROUTE || this.vehiclestate == VehicleState.PARKED || this.vehiclestate == VehicleState.ACQUIRING || this.vehiclestate == VehicleState.DEPOSITING)
            {
                isAssignedOLD = true;
            }
            return isAssignedOLD;
        }
        public void InnerPublishEvent(VehicleEvent.VehicleEventType type)
        {
            base.PublishEvent(VehicleEvent.FormSubject(this.elementId), new VehicleEvent(this.ownerId, EquipmentType.ValueToString(EquipmentType.LIFTER), base.ID, type));
        }
        private void CimAllCmdReset()
        {
        }

        public void SetCSTIDtoPLC(string CSTID)
        {
            CSTID = CSTID.ToUpper();
            //this.I_SHUTTLEMATERIALID = CSTID;
        }
    }
}
